package cst.neu.yuejian_31.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.ClubCollectBean;
import cst.neu.yuejian_31.bean.ClubStateBean;
import cst.neu.yuejian_31.fragment.CIntroFragment;
import cst.neu.yuejian_31.fragment.CCourseFragment;
import cst.neu.yuejian_31.fragment.CVideoFragment;
import cst.neu.yuejian_31.iface.ClubCollectListener;
import cst.neu.yuejian_31.iface.ClubCollectStateListener;
import cst.neu.yuejian_31.model.ClubCollectModel;
import cst.neu.yuejian_31.model.ClubCollectStateModel;

public class ClubDetailActivity extends BaseActivity implements View.OnClickListener,ClubCollectStateListener,ClubCollectListener {

    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private TextView clubName;
    private String clubId,userId;
    private CCourseFragment ccourseFragment=null;
    private CIntroFragment cIntroFragment=null;
    private CVideoFragment cVideoFragment=null;
    private DisplayMetrics dm;
    private int state;
    private int collectState;
    private String s;
    private ImageView favorImageButton,btn_return;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        btn_return= (ImageView) findViewById(R.id.imageButton_return_set);
        init();
        initviews();

        ClubCollectStateModel model=new ClubCollectStateModel();
        model.getClubState(userId,clubId,this);
        favorImageButton.setOnClickListener(this);
        btn_return.setOnClickListener(this);

    }

    private void initviews() {
        Intent intent=getIntent();
//        Toast.makeText(this,intent.getStringExtra("club_name"), Toast.LENGTH_SHORT).show();
        clubId=intent.getStringExtra("club_id");
        userId=getUserId();
//        Toast.makeText(this, clubId+userId, Toast.LENGTH_SHORT).show();
        clubName= (TextView) findViewById(R.id.textView_clubname);
        favorImageButton= (ImageView) findViewById(R.id.imageView_club_collect);
        clubName.setText(intent.getStringExtra("club_name"));
    }

    private void init() {
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager=(ViewPager)findViewById(R.id.viewPager_club_detail);
        viewpager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        pagersliding=(PagerSlidingTabStrip) findViewById(R.id.pagerslidingtabstrip_club_detail);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();//设置PagerSlidingTabStrip显示效果

    }
    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 3, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#7FB12E"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
//        pagersliding.setSelectedTextColor(Color.parseColor("#7FB12E"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色
    }

    @Override
    public void onSuccess(ClubStateBean clubStateBean) {
        state=clubStateBean.getState();
//        s=String.valueOf(state);
   Toast.makeText(ClubDetailActivity.this,s, Toast.LENGTH_SHORT).show();
        if(state==1){
            favorImageButton.setImageResource(R.drawable.ic_collect);

        }else {
            favorImageButton.setImageResource(R.drawable.ic_uncollect);
        }
    }

    @Override
    public void onSuccess(ClubCollectBean clubCollectBean) {
        collectState=clubCollectBean.getSuccess();
        if(collectState==1){
            favorImageButton.setImageResource(R.drawable.ic_collect);
            Toast.makeText(ClubDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();

        }else {
            favorImageButton.setImageResource(R.drawable.ic_uncollect);
            Toast.makeText(ClubDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_club_collect:
                doFavor();
                break;
            case R.id.imageButton_return_set:
                finish();
                break;
        }
    }

    private void doFavor() {
        ClubCollectModel favorModel=new ClubCollectModel();
        favorModel.clubCollect(userId,clubId,this);
    }

    //自定义ViewPagerAdapter子类
    private class PagerAdapter extends FragmentPagerAdapter {
        private String[] titles={"俱乐部简介","精品课程","俱乐部视频"};//显示在二级导航上的标题文字
        public PagerAdapter(FragmentManager fm)    {  super(fm);    }
        @Override
        public CharSequence getPageTitle(int position) {return titles[position]; }
        public int getCount()
        {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) { //根据位置返回具体某个导航上对应的Fragment
            switch (position)
            {   case 0: if(cIntroFragment==null) {  cIntroFragment=new CIntroFragment();  }
                return cIntroFragment;
                case 1:
                if(ccourseFragment==null)  {  ccourseFragment=new CCourseFragment();}
                return ccourseFragment;
                case 2: if(cVideoFragment==null) {  cVideoFragment=new CVideoFragment();  }
                    return cVideoFragment;
                default: return null;
            }
        }
    }
}
